﻿using System.ComponentModel.Composition;
using Microsoft.Office.Interop.Word;

namespace Songhay.Office2010.WordWalkingStick.Commands
{
    using Songhay.Models;
    using Songhay.Office2010.Word;

    /// <summary>
    /// Customizes Office Word styles,
    /// for Quick Styles and HTML markup.
    /// </summary>
    [Export(typeof(IOfficeWordCommand))]
    public class InsertContentControlCommand : IOfficeWordCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InsertContentControlCommand"/> class.
        /// </summary>
        public InsertContentControlCommand()
        {
            this.Name = "InsertContentControl";
        }

        #region IOfficeWordCommand Members

        public string Name { get; set; }

        public void ExecuteCommand<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            var input = message as IControlInteractionMessage;
            if(input == null) return;

            var range = wordApplication.Selection.Range;

            //Prevent nesting:
            if(range.ParentContentControl != null) return;

            switch(input.ControlId)
            {
                case "InsertContentControlAmazonImage":
                    InsertContentControlAmazonImage(wordApplication, range);
                    break;
                case "InsertContentControlImage":
                    InsertContentControlImage(wordApplication, range);
                    break;
            }

            if(input.ControlId.StartsWith("InsertContentControlAcronym"))
            {
                InsertContentControlAcronym(wordApplication, range, (string)input.ControlValue);
            }
            if(input.ControlId.StartsWith("InsertContentControlCssBlock"))
            {
                InsertContentControlCssBlock(wordApplication, range, (string)input.ControlValue);
            }
        }

        public object ExecuteCommandForBinding<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            throw new System.NotImplementedException();
        }

        #endregion

        static void InsertContentControlAcronym(Application wordApplication, Range range, string controlValue)
        {
            wordApplication.Selection.BoldRun();

            var tag = "rxAcronym";
            var array = controlValue.Split(',');
            var acronym = array[0].ToString();
            var text = array[1].Trim();

            range.InsertAfter(" ");
            var controlAcronym = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlText);
            controlAcronym.Tag = tag;
            controlAcronym.Title = text;
            controlAcronym.Range.Text = acronym;
            controlAcronym.LockContents = true;
        }

        static void InsertContentControlAmazonImage(Application wordApplication, Range range)
        {
            wordApplication.Selection.BoldRun();

            //range.InsertAfter("  "); //VBA experiments show that two spaces works as expected.

            var tag = "rxAmazonProduct";

            range.InsertAfter("Amazon Product" + WordCharacters.CrLf);

            range.InsertAfter("ASIN:  ");
            var controlAsin = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlText);
            controlAsin.SetDefaultTextStyle("HTML Code");
            controlAsin.Tag = tag + "Asin";
            controlAsin.Title = "ASIN";

            range.InsertAfter(WordCharacters.CrLf + "CSS:  ");
            var controlCss = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlComboBox);
            controlCss.SetDefaultTextStyle("HTML Code");
            controlCss.Tag = tag + "Css";
            controlCss.Title = "CSS";
            controlCss.DropdownListEntries
                .Add("float:left;margin:16px;", "css1");
            controlCss.DropdownListEntries
                .Add("float:right;margin:16px;", "css2");
            controlCss.DropdownListEntries
                .Add("margin:16px;", "css3");

            range.InsertAfter(WordCharacters.CrLf + "Image Path:  ");
            var controlImagePath = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlText);
            controlImagePath.SetDefaultTextStyle("HTML Code");
            controlImagePath.Tag = tag + "ImagePath";
            controlImagePath.Title = "Image Path";

            var controlWrapper = range.ContentControls
                .Add(WdContentControlType.wdContentControlRichText);
            controlWrapper.LockContents = true;
            controlWrapper.Tag = tag;
            controlWrapper.Title = "Amazon";

            controlAsin.Range.Select();
        }

        static void InsertContentControlCssBlock(Application wordApplication, Range range, string controlValue)
        {
            range.Expand(WdUnits.wdParagraph);

            var tag = "rxCssBlock";

            var controlCssBlock = range
                .ContentControls
                .Add(WdContentControlType.wdContentControlRichText);
            controlCssBlock.Tag = tag;
            controlCssBlock.Title = controlValue;
            controlCssBlock.LockContents = false;
        }

        static void InsertContentControlImage(Application wordApplication, Range range)
        {
            wordApplication.Selection.BoldRun();

            var tag = "rxImage";

            range.InsertAfter("Web Image" + WordCharacters.CrLf);

            range.InsertAfter("Image Path:  ");
            var controlImagePath = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlText);
            controlImagePath.SetDefaultTextStyle("HTML Code");
            controlImagePath.Tag = tag + "Path";
            controlImagePath.Title = "Image Path";
            controlImagePath.Range.Text = "http://kintespace.com/bitmaps/";

            range.InsertAfter(WordCharacters.CrLf + "Image Alternative Text:  ");
            var controlImageAlt = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlText);
            controlImageAlt.SetDefaultTextStyle("HTML Code");
            controlImageAlt.Tag = tag + "Alt";
            controlImageAlt.Title = "Image Alternative Text";

            range.InsertAfter(WordCharacters.CrLf + "CSS:  ");
            var controlCss = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlComboBox);
            controlCss.SetDefaultTextStyle("HTML Code");
            controlCss.Tag = tag + "Css";
            controlCss.Title = "CSS";
            controlCss.DropdownListEntries
                .Add("float:left;margin:16px;", "css1");
            controlCss.DropdownListEntries
                .Add("float:right;margin:16px;", "css2");
            controlCss.DropdownListEntries
                .Add("display:block;margin:16px;margin-left:auto;margin-right:auto", "css3");
            controlCss.DropdownListEntries
                .Add("display:block;margin:16px;margin-left:auto;margin-right:auto;max-width:512px", "css4");

            range.InsertAfter(WordCharacters.CrLf + "Image Link:  ");
            var controlImageLink = range.Characters
                .Last.ContentControls
                .Add(WdContentControlType.wdContentControlText);
            controlImageLink.SetDefaultTextStyle("HTML Code");
            controlImageLink.Tag = tag + "Link";
            controlImageLink.Title = "Image Link";

            var controlWrapper = range.ContentControls
                .Add(WdContentControlType.wdContentControlRichText);
            controlWrapper.LockContents = true;
            controlWrapper.Tag = tag;
            controlWrapper.Title = "Image";

            controlImagePath.Range.Select();

        }
    }
}
